home *** CD-ROM | disk | FTP | other *** search
- /*
- HASLongControls.c from Hsoi's App Shell ©1995-1997 John C. Daub. All rights reserved.
-
- This file is based upon the WASTE Demo App's LongControls.c by Marco Piovanelli.
- The functions here help us to properly deal with some "short-comings" (there's
- a lame pun in there) of the MacOS.
-
- Normally, controls use shorts to hold the values of the controls. Unfortunately,
- for scroll bars this is a problem. Since WASTE allows us to have more than
- 32k of text, we could conceivably have scroll bar values that would need to be
- larger than the upper limit of a 2-byte signed short (32,767). These functions
- herein allow us to use 4-byte signed longs to retain the value of the scroll
- bars. Furthermore, these functions allow us to co-exist happily with our
- longs and the MacOS.
-
- So, give these a look over, and see how they're implimented throughout the
- rest of the app shell. You'll see how they work.
- */
-
- #pragma mark ••• #includes •••
-
- #ifndef _WASTE_
- #include "WASTE.h"
- #endif
- #include "HASGlobals.h"
- #ifndef __HSOIS_APP_SHELL__
- #include "HASMain.h"
- #endif
- #include "HASLongControls.h"
- #include "HASUtilities.h"
-
-
- #pragma mark -
- #pragma mark ••• Creation/Destruction •••
-
- /*
- * This will create our long control
- */
-
- OSErr HsoiLCAttach( ControlRef control )
- {
- Handle aux = nil;
- LCAuxPtr pAux;
- OSErr reply = noErr;
-
- /* allocate the auxiliary record that will hold long settings */
-
- aux = NewHandleClear( sizeof( LCAuxRec ) );
- if ( aux == nil )
- {
- reply = MemError();
- return reply;
- }
-
- /* store a handle to the auxiliary record in the contrlRfCon field */
-
- SetControlReference( control, (long)aux);
-
- /* copy current control settings into the auxiliary record */
-
- pAux = *((LCAuxHandle)aux);
- pAux->value = GetControlValue( control );
- pAux->min = GetControlMinimum( control );
- pAux->max = GetControlMaximum( control );
-
- return reply;
- }
-
-
- // detroy a long control
-
- void HsoiLCDetach( ControlRef control )
- {
- Handle aux;
-
- aux = (Handle)GetControlReference( control );
-
- if ( aux != nil )
- {
- SetControlReference( control, 0 );
-
- HsoiForgetHandle( &aux );
- }
-
- return;
- }
-
- #pragma mark -
- #pragma mark ••• Set Values •••
-
- // set the value of a long control
-
- void HsoiLCSetValue( ControlRef control, long value )
- {
- LCAuxPtr pAux;
- short controlMin, controlMax, newControlValue;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure value is in the range min...max */
-
- if ( value < pAux->min )
- value = pAux->min;
- if ( value > pAux->max )
- value = pAux->max;
-
- /* save value in auxiliary record */
-
- pAux->value = value;
-
- /* calculate new thumb position */
-
- controlMin = GetControlMinimum( control );
- controlMax = GetControlMaximum( control );
- newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min,
- pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));
-
- /* do nothing if the thumb position hasn't changed */
-
- if ( newControlValue != GetControlValue( control ) )
- SetControlValue( control, newControlValue );
-
- return;
- }
-
- // set the minimum value of a long control
-
- void HsoiLCSetMin( ControlRef control, long min )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure min is less than or equal to max */
-
- if ( min > pAux->max )
- min = pAux->max;
-
- /* save min in auxiliary record */
- pAux->min = min;
-
- /* set contrlMin field to min or MINSHORT, whichever is greater */
-
- if ( min < MINSHORT )
- min = MINSHORT;
-
- SetControlMinimum( control, min );
-
- /* reset value */
-
- HsoiLCSetValue( control, pAux->value );
-
- return;
- }
-
- // set the maximum value of a long control
-
- void HsoiLCSetMax( ControlRef control, long max )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure max is greater than or equal to min */
-
- if ( max < pAux->min )
- max = pAux->min;
-
- /* save max in auxiliary record */
-
- pAux->max = max;
-
- /* set contrlMax field to max or MAXSHORT, whichever is less */
-
- if ( max > MAXSHORT )
- max = MAXSHORT;
-
- SetControlMaximum( control, max );
-
- /* reset value */
-
- HsoiLCSetValue( control, pAux->value );
-
- return;
- }
-
-
- #pragma mark -
- #pragma mark ••• Get Functions •••
-
- // in the next 3 functions (HsoiLCGetValue/Min/Max), there is a line of commented out code
- // at the end of each function. All this line of code is is another way of writing the
- // function (essentially, 1 line instead of 2). The commented out line DOES yield tighter
- // and smaller code, but it a little harder to read. Use either (but not both).
-
- // get the value of a long control
-
- long HsoiLCGetValue( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
- return pAux->value;
-
- // return (*(LCAuxHandle)GetControlReference(control))->value;
-
- }
-
-
- // get the minimum of a long control
-
- long HsoiLCGetMin( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
- return pAux->min;
-
- // return (*(LCAuxHandle)GetControlReference(control))->min;
- }
-
-
- // get the maximum of a long control
-
- long HsoiLCGetMax( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
- return pAux->max;
-
- // return (*(LCAuxHandle)GetControlReference(control))->max;
- }
-
-
- #pragma mark -
- #pragma mark ••• Synch •••
-
- // synchronizing long settings with control (short) settings
-
- void HsoiLCSynch( ControlRef control )
- {
- LCAuxPtr pAux;
- short controlMin, controlMax, controlValue;
-
- controlMin = GetControlMinimum( control );
- controlMax = GetControlMaximum( control );
- controlValue = GetControlValue( control );
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* calculate new long value */
-
- pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
- controlMax - controlMin), pAux->max - pAux->min );
-
- return;
- }
-